﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DTO;

namespace DAO
{
    public static class TinMacCaDAO
    {
        public static TinMacCaDTO LayTinBanTuDataRow(DataRow dr)
        {
            TinMacCaDTO tmc = null;
            try
            {
                tmc = new TinMacCaDTO();
                tmc.MaTinMacCa = dr["MaTinMacCa"].ToString();
                tmc.NgayGui = DateTime.Parse(dr["NgayGui"].ToString());
                tmc.MaTaiKhoanGui = dr["MaTaiKhoanGui"].ToString();
                tmc.MaTinBan = dr["MaTinBan"].ToString();
                tmc.MaTaiKhoanNhan = dr["MaTaiKhoanNhan"].ToString();
                tmc.SoLuongMua = int.Parse(dr["SoLuongMua"].ToString());
                tmc.GiaNiemYet = int.Parse(dr["GiaNiemYet"].ToString());
                tmc.GiaDeNghiTra = int.Parse(dr["GiaDeNghiTra"].ToString());
                tmc.TinNhan = dr["TinNhan"].ToString();
                tmc.HanTraLoi = DateTime.Parse(dr["HanTraLoi"].ToString());
                tmc.ChoPhepMacCa = bool.Parse(dr["ChoPhepMacCa"].ToString());
            }
            catch (Exception ex)
            {
                throw new Exception("Lỗi khi lấy thông tin mặc cả: " + ex.ToString());
            }
            return tmc;
        }

        public static IList<SqlParameter> TruyenThamSoChoStored(TinMacCaDTO item)
        {
            IList<SqlParameter> arrParameter = new List<SqlParameter>();
            arrParameter.Add(new SqlParameter("@MaTinMacCa", item.MaTinBan));
            arrParameter.Add(new SqlParameter("@NgayGui", item.NgayGui));
            arrParameter.Add(new SqlParameter("@MaTaiKhoanGui", item.MaTaiKhoanGui));
            arrParameter.Add(new SqlParameter("@MaTinBan", item.MaTinBan));
            arrParameter.Add(new SqlParameter("@MaTaiKhoanNhan", item.MaTaiKhoanNhan));
            arrParameter.Add(new SqlParameter("@SoLuongMua", item.SoLuongMua));
            arrParameter.Add(new SqlParameter("@GiaNiemYet", item.GiaNiemYet));
            arrParameter.Add(new SqlParameter("@GiaDeNghiTra", item.GiaDeNghiTra));
            arrParameter.Add(new SqlParameter("@TinNhan", item.TinNhan));
            arrParameter.Add(new SqlParameter("@HanTraLoi", item.HanTraLoi));
            arrParameter.Add(new SqlParameter("@ChoPhepMacCa", item.ChoPhepMacCa));
            return arrParameter;
        }

        public static void ThemTinMacCa(TinMacCaDTO tmc)
        {
            try
            {
                IList<SqlParameter> arrParameter = TruyenThamSoChoStored(tmc);
                DataTable dt = DataProvider.ExecuteStoreProc("addTinMacCa", arrParameter);
            }
            catch (Exception ex)
            {
                throw new Exception("Lỗi khi thêm mới tin mặc cả " + ex.ToString());
            }
        }

        public static TinMacCaDTO getTinMacCa(string manguoiban)
        {
            try
            {
                IList<SqlParameter> arrParameter = new List<SqlParameter>();
                arrParameter.Add(new SqlParameter("@MaNguoiBan", manguoiban));
                DataTable dt = DataProvider.ExecuteStoreProc("getTinMacCaMoiTheoNguoiBan", arrParameter);
                if (dt.Rows.Count > 0)
                    return LayTinBanTuDataRow(dt.Rows[0]);
            }
            catch (Exception ex)
            {
                throw new Exception("Lỗi khi lấy tin mac ca " + ex.ToString());
            }
            return null;
        }
    }
}
